<?php

namespace app\admin\model\sport;

use think\Model;
use app\admin\model\sport\Area;
use app\admin\model\User;


class Batch extends Model
{

    

    

    // 表名
    protected $name = 'sport_batch';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'score_type_text',
        'over_time_text'
    ];
    

    
    public function getScoreTypeList()
    {
        return ['middle' => __('Score_type middle'), 'senior' => __('Score_type senior')];
    }


    public function getScoreTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['score_type']) ? $data['score_type'] : '');
        $list = $this->getScoreTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOverTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['over_time']) ? $data['over_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setOverTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function area()
    {
        return $this->belongsTo(Area::class, 'area_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
